package java.com;

import com.postgresql.PostgreSqlApplication;
import com.postgresql.dto.Grid;
import com.postgresql.dto.Poi;
import com.postgresql.service.GridService;
import com.postgresql.service.PoiService;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.MultiPolygon;
import com.vividsolutions.jts.geom.Polygon;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

/**
 * @author: 顾志杰
 */

@RunWith(SpringRunner.class)
@SpringBootTest(classes = PostgreSqlApplication.class)
@Slf4j
public class test {

    @Autowired
    private PoiService poiService;

    @Autowired
    private GridService gridService;

    @Test
    public void insertPoi() throws Exception{
        poiService.insert();
    }
    @Test
    public void insertGrid() throws Exception{
        gridService.insert();
    }


    @Test
    public void queryGrid(){

        List<Grid> all = gridService.findAll();
        all.forEach(poi -> {
            MultiPolygon geom = poi.getGeom();
            Polygon[] polygons = new Polygon[geom.getNumGeometries()];
            for (int i = 0; i < polygons.length; i++) {
                Geometry geometry = geom.getGeometryN(i);
                Polygon polygon = (Polygon) geometry;
                Coordinate[] coordinates2 = polygon.getCoordinates();
                int size2 = coordinates2.length;
                for (int j = 0; j < size2; j++) {
                    double x = coordinates2[j].x;
                    double y = coordinates2[j].y;
                    log.info("查询成功 id:{} x:{},y:{}",poi.getGid(),x,y);
                }
            }
            System.out.println();
        });
    }


    @Test
    public void query(){
        double d = 14093101.5979161;

        List<Poi> query = poiService.query();
        query.forEach(poi -> {
            System.out.println("-------------id=" + poi.getGid() + ";x = " + d  + ";y=" + poi.getGeom().getY());
        });
    }

}
